package morenoapp.tools;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import morenoapp.util.MorenoUtil;
import org.apache.commons.io.FileUtils;
import org.jdesktop.application.Action;

/**
 *
 * @author dmitry.mamonvo
 */
public class MorenoTool {
    public static void updateResources(File _base, Class<?> _clazz) throws IOException {
        File resourceFile = getCorrespondingResourceFileName(_base, _clazz);
        if (resourceFile.exists()==false){
            resourceFile.getParentFile().mkdirs();
            resourceFile.createNewFile();
        }
        List<String> resorcesLines = FileUtils.readLines(resourceFile, "UTF-8");

        //add generated body
        for (Method m : MorenoUtil.filterAndSortMethodsByOrder(_clazz.getMethods())) {
            if (m.getAnnotation(Action.class) != null) {
                boolean resourcePresented = false;
                for (String resLine : resorcesLines) {
                    if (resLine.contains(m.getName() + ".Action")) {
                        resourcePresented = true;
                        break;
                    }
                }
                if (resourcePresented == false) {
                    resorcesLines.add("######################################################");
                    resorcesLines.add(m.getName() + ".Action.text = " + m.getName());
                    resorcesLines.add(m.getName() + ".Action.icon = #action_.png");
                    resorcesLines.add(m.getName() + ".Action.accelerator = ");
                    resorcesLines.add(m.getName() + ".Action.shortDescription = ");
                    resorcesLines.add("");
                }
            }
        }
        for(String line:resorcesLines){
            System.out.println(line);
        }
        FileUtils.writeLines(resourceFile, "UTF-8", resorcesLines);
    }

    public static File getCorrespondingResourceFileName(File _base, Class<?> _clazz) {
        File resourceFile = new File(_base, _clazz.getPackage().getName().replace(".", "/") + "/resources/" + _clazz.getSimpleName() + ".properties");
        return resourceFile;
    }

    public static void updateSourceFile(Class<?> _clazz, List<String> _generatedCode) throws IOException {
        File javaFile = new File("src/" + _clazz.getName().replace(".", "/") + ".java");
        List<String> javaFileLines = FileUtils.readLines(javaFile);
        List<String> javaFileGenerated = new ArrayList<String>();
        //add head lines
        boolean headMatched = false;
        for (String line : javaFileLines) {
            javaFileGenerated.add(line);
            if (line.trim().equals("//generated:begin")) {
                headMatched = true;
                break;
            }
        }
        if (headMatched == false) {
            throw new RuntimeException("Source code has no start tag");
        }

        //add generated lines
        javaFileGenerated.addAll(_generatedCode);

        //add file tail
        boolean tailMatched = false;
        for (String line : javaFileLines) {
            if (line.trim().equals("//generated:end")) {
                tailMatched = true;
            }
            if (tailMatched == true) {
                javaFileGenerated.add(line);
            }
        }
        if (tailMatched == false) {
            throw new RuntimeException("Source code has no end tag");
        }
        FileUtils.writeLines(javaFile, javaFileGenerated);
    }
}
